package com.iuweb.login.service.impl;

import com.iuweb.commen.domain.ShareUserResources;
import com.iuweb.login.mapper.AuthRoleMapper;
import com.iuweb.login.mapper.LoginMapper;
import com.iuweb.login.model.*;
import com.iuweb.login.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ruiwu
 * @version V1.0
 * @Description:
 */
@Service
public class LoginServiceImpl implements LoginService {

   @Autowired
   private LoginMapper loginMapper;
   @Autowired
   private AuthRoleMapper authRoleMapper;

    public User findUserByUsername(String username) {
        return loginMapper.findUserByUsername(username);
    }

    /**
     * 获取登录结果
     * @param userName
     * @param password
     * @return
     */
    public List<ShareUserResources> getLoginResult(String userName, String password) {
        return  login(userName, password);
    }

    /**
     * 返回目录信息
     * @param userName
     * @param password
     * @return
     */
    private List<ShareUserResources> login(String userName, String password){
        List<ShareUserResources> shareUserResourcesList = new ArrayList<ShareUserResources>();// 放入userdetail
                List<AuthResource> authResource = authRoleMapper.getResource();
        for (AuthResource  resource : authResource) {
            if (resource != null) {// 数据问题，排除异常可能
                ShareUserResources shareUserResources = new ShareUserResources();
                shareUserResources.setParentRefid(resource.getParentRefid());
                shareUserResources.setRefid(resource.getRefid());
                shareUserResources.setResourceName(resource.getResourceName());
                shareUserResources.setResourceType(resource.getResourceType());
                shareUserResources.setResourceUrl(resource.getResourceUrl());
                shareUserResources.setUniqueCode(resource.getUniqueCode());
                shareUserResources.setIconClass(resource.getIconClass());
                shareUserResourcesList.add(shareUserResources);
            }
        }
        return shareUserResourcesList;
    }
}
